﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace DHYHN.ASPNET.HoiNghi.UserControls
{
    public partial class ReportUserControl : System.Web.UI.UserControl
    {
        public string sqlConnectionstring;
        public string selectQuery;
        public int pageSize=8;
        SqlDataAdapter dataAdapter;
        DataSet dataSet = new DataSet();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dataAdapter = new SqlDataAdapter(selectQuery, sqlConnectionstring);
                setMax();
                LoadDataTiDataList(0, pageSize);
            }
        }
        public ReportUserControl(string sqlCon, string selectQuery)
        {
            dataAdapter = new SqlDataAdapter(selectQuery, sqlConnectionstring);
        }
        public ReportUserControl()
        {
            dataAdapter = new SqlDataAdapter(selectQuery, sqlConnectionstring);
        }

        public void LoadDataTiDataList(int start, int count)
        {
            dataAdapter = new SqlDataAdapter(selectQuery, sqlConnectionstring);
            dataAdapter.Fill(dataSet, start, count, "MyProduct");            
            DataList1.DataSource = dataSet.Tables["MyProduct"].DefaultView;
            DataList1.DataBind();
            if (DataList1.Items.Count > 0)
            {
                   lblStart.Text = (start + 1).ToString();
                   lblEnd.Text = (start + DataList1.Items.Count).ToString();
                   lblStartTop.Text = lblStart.Text;
                   lblEndTop.Text = lblEnd.Text;
            }

            if (start + count >= Convert.ToInt32(lblMax.Text))
            {
                lbtNext.Enabled = false;
                LinkButton2.Enabled = false;
                
            }
            else
            {
                lbtNext.Enabled = true;
                LinkButton2.Enabled = true;
            }
            if (start == 0)
            {
                lbtPri.Enabled = false;
                LinkButton3.Enabled = false;
            }
            else
            { lbtPri.Enabled = true;
            LinkButton3.Enabled = true;
            }
        }

        private void setMax()
        {
            dataAdapter = new SqlDataAdapter(selectQuery, sqlConnectionstring);
            dataAdapter.Fill(dataSet, "MyProduct1");
            lblMax.Text= dataSet.Tables["MyProduct1"].Rows.Count.ToString();
            lblMaxTop.Text = lblMax.Text;
        }

        private void NextPage()
        {
            int start = Convert.ToInt32(lblStart.Text)-1;
            int end = Convert.ToInt32(lblEnd.Text);
            int max = Convert.ToInt32(lblMax.Text);
            LoadDataTiDataList(end,pageSize);
        }

        private void BackPage()
        {
            int start = Convert.ToInt32(lblStart.Text);
            int end = Convert.ToInt32(lblEnd.Text);
            int max = Convert.ToInt32(lblMax.Text);
            LoadDataTiDataList(start -1 - pageSize, pageSize);
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            try
            {
                NextPage();
            }
            catch { }
        }

        protected void lbtPri_Click(object sender, EventArgs e)
        {
            try
            {
                BackPage();
            }
            catch { }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }

        protected override void LoadControlState(object savedState)
        {
            object[] list = savedState as object[];
            base.LoadControlState(list[0]);
            //if (list[1] != null)
            //<some varible>= list[1];
        }

        protected override object SaveControlState()
        {
            object[] list = new object[2];
            list[0] = base.SaveControlState();
            //list[1] = <some varible>;
            return list;
        }
    }
}